/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  Implementation of LocationGestureArea. Implements pinch zoom for
*               the Location application map view.
*/

#include "locationgesturearea.h"
#include <QtCore/QDebug>
#include <QtGui/QTouchEvent>
#include <QtGui/QVector2D>

LocationGestureArea::LocationGestureArea( QDeclarativeItem *parent )
    : QDeclarativeItem( parent )
{
    qDebug() << "LocationGestureArea::LocationGestureArea : begin";
    setAcceptTouchEvents( true );
    grabGesture( Qt::PinchGesture );

    // initial values for the map zoom
    lastScaleFactor = 15;
    lastPinchFactor = 1;
    qDebug() << "LocationGestureArea::LocationGestureArea : end";
}

LocationGestureArea::~LocationGestureArea()
{
}

qreal LocationGestureArea::mapScale() const
{
    return lastScaleFactor;
}

void LocationGestureArea::setMapScale( qreal scale )
{
    lastScaleFactor = scale;
}

bool LocationGestureArea::sceneEvent( QEvent *event )
{
    qDebug() << "LocationGestureArea::sceneEvent : begin";


    if ( isEnabled() ) {
        QEvent::Type type = event->type();

        switch( type ) {
            case QEvent::TouchBegin:
            case QEvent::TouchEnd:
            case QEvent::TouchUpdate: {
                event->accept();
                if ( type == QEvent::TouchEnd ) {
                   emit finished();
                }

                QTouchEvent& touch = *static_cast<QTouchEvent*>( event );
                return handleTouchEvent( touch );
             }
             default:
                 break;
             }
         }

    qDebug() << "LocationGestureArea::sceneEvent : end";
    return QDeclarativeItem::sceneEvent( event );
}

bool LocationGestureArea::handleTouchEvent( const QTouchEvent &event )
{

    if ( event.touchPoints().count() < 2 ) {
        return true;
    }

    QTouchEvent::TouchPoint p0 = event.touchPoints().at(0);
    QTouchEvent::TouchPoint p1 = event.touchPoints().at(1);

    switch (event.type()) {
        case QEvent::TouchBegin:
               lastPinchFactor = 1;
               emit started();
            break;
        case QEvent::TouchUpdate: {
            QVector2D now(p1.pos() - p0.pos());
            QVector2D then(p1.lastPos() - p0.lastPos());
            qreal scale = then.length()/now.length();
            lastPinchFactor = lastPinchFactor / scale;
            lastScaleFactor = lastPinchFactor * 100;
            emit mapScaleChanged();
            break;
        }
        default:
            break;
    }

    return true;
}
